package net.brucecooper.mindmap.client;

import gwt.canvas.client.Canvas;

public class VerticalPanel extends Panel {
	public VerticalPanel() {
		super();
	}

	public VerticalPanel(Panel.Alignment alignment) {
		super(alignment);
	}

	
	@Override
    public void layout(Canvas canvas) {
		int y = 0;
		int w = getPreferredWidth(canvas);

		if (children != null) {
			for (int i = 0; i < childCount; i++) {
				GraphicComponent comp = children[i];
				comp.layout(canvas);
				int cw = comp.getPreferredWidth(canvas);
				int ch = comp.getPreferredHeight(canvas);

				int x = 0;
				switch (alignment) {
				case LEFT:
					x = 0;
					break;
				case CENTER:
					x = (w - cw) / 2;
					break;
				case RIGHT:
					x = w - cw;
					break;
				}

				comp.setBounds(x, y, cw, ch);
				y += ch + spacing;
			}
		}
	}

	@Override
    public int getPreferredHeight(Canvas canvas) {
		if (children != null) {
			int height = 0;
			for (int i = 0; i < childCount; i++) {
				GraphicComponent comp = children[i];
				height += comp.getPreferredHeight(canvas);
			}
			height += spacing * (childCount-1);
			return height;
		} else {
			return 0;
		}
	}

	@Override
    public int getPreferredWidth(Canvas canvas) {
		if (children != null) {
			int max = 0;
			for (int i = 0; i < childCount; i++) {
				GraphicComponent comp = children[i];
				int h = comp.getPreferredWidth(canvas);
				if (h > max)
					max = h;
			}
			return max;
		} else {
			return 0;
		}
	}

}
